//
//  FactoryClass.m
//  ECTECTIphone
//
//  Created by mac on 15/5/21.
//
//

#import "FactoryClass.h"
#import "AesEncrypt.h"
#import <CommonCrypto/CommonDigest.h>
#import <ifaddrs.h>
#import <arpa/inet.h>
#import <UIKit/UIKit.h>

@implementation FactoryClass

+(BOOL)validateMobile:(NSString *)mobileNum{
    /**
     * 手机号码
     * 移动：134[0-8],135,136,137,138,139,150,151,157,158,159,182,187,188
     * 联通：130,131,132,152,155,156,185,186
     * 电信：133,1349,153,180,189,181(增加)
     */
    NSString * MOBIL = @"^1(3[0-9]|5[0-35-9]|8[025-9]|7[0-9])\\d{8}$";
    /**
     10         * 中国移动：China Mobile
     11         * 134[0-8],135,136,137,138,139,150,151,157,158,159,182,187,188
     12         */
    NSString * CM = @"^1(34[0-8]|(3[5-9]|5[017-9]|8[2378])\\d)\\d{7}$";
    /**
     15         * 中国联通：China Unicom
     16         * 130,131,132,152,155,156,185,186
     17         */
    NSString * CU = @"^1(3[0-2]|5[256]|8[56])\\d{8}$";
    /**
     20         * 中国电信：China Telecom
     21         * 133,1349,153,180,189,181(增加)184
     22         */
    NSString * CT = @"^1((33|53|8[0194])[0-9]|349)\\d{7}$";
    
    NSPredicate *regextestmobile = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", MOBIL];
    NSPredicate *regextestcm = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CM];
    NSPredicate *regextestcu = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CU];
    NSPredicate *regextestct = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CT];
    
    if (([regextestmobile evaluateWithObject:mobileNum]
         || [regextestcm evaluateWithObject:mobileNum]
         || [regextestct evaluateWithObject:mobileNum]
         || [regextestcu evaluateWithObject:mobileNum])) {
        return YES;
    }
    
    return NO;
}


//+(BOOL)validateMobile:(NSString *)mobileNum
//{
//    
//    if([mobileNum hasPrefix:@"1"] && mobileNum.length == 11)
//    {
//        
//        return YES;
//        
//    }
//        return NO;
////
////    /**
////     * 手机号码
////     * 移动：134[0-8],135,136,137,138,139,150,151,157,158,159,182,187,188
////     * 联通：130,131,132,152,155,156,185,186
////     * 电信：133,1349,153,180,189
////     */
////    NSString * MOBILE = @"^1(3[0-9]|5[0-35-9]|8[0235-9])\\d{8}$";
////    /**
////     10         * 中国移动：China Mobile
////     11         * 134[0-8],135,136,137,138,139,150,151,157,158,159,182,187,188
////     12         */
////    NSString * CM = @"^1(34[0-8]|(3[5-9]|5[017-9]|8[278])\\d)\\d{7}$";
////    /**
////     15         * 中国联通：China Unicom
////     16         * 130,131,132,152,155,156,185,186   添加联通145 号段
////     17         */
////    NSString * CU = @"^1(3[0-2]|5[256]|8[56]|4[5])\\d{8}$";
////    /**
////     20         * 中国电信：China Telecom
////     21         * 133,1349,153,180,189,181(增加)
////     22         */
////    NSString * CT = @"^1((33|53|8[019])[0-9]|349)\\d{7}$";
////    /**
////     25         * 大陆地区固话及小灵通
////     26         * 区号：010,020,021,022,023,024,025,027,028,029
////     27         * 号码：七位或八位
////     28         */
////    // NSString * PHS = @"^0(10|2[0-5789]|\\d{3})\\d{7,8}$";
////    
////    NSPredicate *regextestmobile = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", MOBILE];
////    NSPredicate *regextestcm = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CM];
////    NSPredicate *regextestcu = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CU];
////    NSPredicate *regextestct = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CT];
////    
////    if (([regextestmobile evaluateWithObject:mobileNum] == YES)
////        || ([regextestcm evaluateWithObject:mobileNum] == YES)
////        || ([regextestct evaluateWithObject:mobileNum] == YES)
////        || ([regextestcu evaluateWithObject:mobileNum] == YES))
////    {
////        return YES;
////    }
////    else
////    {
////        return NO;
////    }
//}


+(BOOL)validateNumber:(NSString *)textString
{
    NSString *number = @"^[0-9]+$";
    NSPredicate *numberPre = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", number];
    return [numberPre evaluateWithObject:textString];
}

+ (BOOL)validateEmail:(NSString *)email
{
    NSString *emailRegex = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
    NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];
    return [emailTest evaluateWithObject:email];
}


/**
 *  文本只能输入汉字数字字母
 *
 *  @param inputstr 穿参
 *
 *  @return YES:符合正则 NO:不符合
 */
+ (BOOL)containNumberCharactorChinese:(NSString *)inputstr
{
    NSString *regex = @"[a-zA-Z\u4e00-\u9fa5][a-zA-Z0-9\u4e00-\u9fa5]+$";
    NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
    
    if(![pred evaluateWithObject:inputstr])
    {
        return NO;
    }
    else
    {
        return YES;
    }
}


//+(MBProgressHUD*)HUDShowMessage:(NSString*)msg addedToView:(UIView*)view
//{
//    static MBProgressHUD* hud = nil;
//    if (!hud) {
//        hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
//    }
//    hud.mode = MBProgressHUDModeText;
//    hud.labelText = msg;
//    hud.hidden = NO;
//    hud.alpha = 1.0f;
//    [hud hide:YES afterDelay:1.0f];
//    return hud;
//}


/**
 *  只能输入中文,英文，数字,下划线，减号
 *
 *
 *  return YES:符合要求 NO:不符合要求
 */
+ (BOOL)containNumberChineseEnglishUnderlineAndMinus:(NSString *)inputstr
{
    NSString *regex = @"^[-_a-zA-Z0-9\u4e00-\u9fa5]+$";
    NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
    return [pred evaluateWithObject:inputstr];
}
/**
 *  至少输入一个数字和一个字母
 *
 *
 *  @return YES:符合 NO:不符合
 */
+ (BOOL)containAtLeastOneNumberAndOneCharactor:(NSString*)inputstr
{
    NSString *regex = @"^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]+$";
    NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
    return [pred evaluateWithObject:inputstr];
}

//只包含中文
+ (BOOL)containChineseOnly:(NSString *)inputstr {
    NSString *regex = @"^[\u4e00-\u9fa5]+$";
    NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
    return [pred evaluateWithObject:inputstr];
}

//获取设备型号
+(NSString *)getDeviceType
{
    return [[UIDevice currentDevice] model];
}

//获取osname
+(NSString *)getOSName
{
    return @"iOS";
}

//获取证通APP版本号

+(NSString *)getBbVersion
{
    NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
    NSString *app_Version = [infoDictionary objectForKey:@"CFBundleShortVersionString"];
    
    return app_Version;
}

////获取系统版本

+(NSString *)getphoneVersion
{
    return [[UIDevice currentDevice] systemVersion];
}

//获取手机 IP
+(NSString *)getIPAddress
{
    NSString *address = @"error";
    struct ifaddrs *interfaces = NULL;
    struct ifaddrs *temp_addr = NULL;
    int success = 0;
    
    // retrieve the current interfaces - returns 0 on success
    success = getifaddrs(&interfaces);
    if (success == 0) {
        // Loop through linked list of interfaces
        temp_addr = interfaces;
        while (temp_addr != NULL) {
            if( temp_addr->ifa_addr->sa_family == AF_INET) {
                // Check if interface is en0 which is the wifi connection on the iPhone
                if ([[NSString stringWithUTF8String:temp_addr->ifa_name] isEqualToString:@"en0"]) {
                    // Get NSString from C String
                    address = [NSString stringWithUTF8String:inet_ntoa(((struct sockaddr_in *)temp_addr->ifa_addr)->sin_addr)];
                }
            }
            
            temp_addr = temp_addr->ifa_next;
        }
    }
    
    // Free memory
    freeifaddrs(interfaces);
    
    return address;
}

+ (NSString* )ShowContentByStr:(NSString*)str Type:(ReplaceType)type{
    
    NSMutableString * string = [[NSMutableString alloc]initWithString:str];
    NSRange range;
    NSInteger replaceLength = 0;
    NSString* changeStr = @"";
    
    str = [str stringByReplacingOccurrencesOfString:@" " withString:@""];
    if ([str length] == 0) {
        return @"";
    }
    
    switch (type) {
        case ReplaceType_name:
        {
            if ([str length] == 1) {
                return [NSString stringWithFormat:@"*%@",str];
            } else if ([str length] == 2){
                replaceLength = 1;
                range = NSMakeRange(0, 1);
            } else if ([str length] >= 3){
                
                replaceLength = 1;
                range = NSMakeRange(1, [str length] - 2);
            } else{
                replaceLength = [str length] - 1 ;
                range = NSMakeRange(0 , replaceLength);
            }
            
            
        }
            break;
        case ReplaceType_ID:
        {
            replaceLength = [str length] - 2;
            range = NSMakeRange(1, replaceLength);
            
        }
            break;
        case ReplaceType_telephone:
        {
            replaceLength = 4;
            range = NSMakeRange(3, 4);
            
        }
            break;
        case ReplaceType_mail:
        {
            NSArray *arr = [string componentsSeparatedByString:@"@"];
            if ([arr count] != 0) {
                NSString *Rstring = arr[0];
                
                if ([Rstring length]<= 2) {
                    
                    Rstring = [Rstring stringByAppendingString:@"****"];
                    
                }else{
                    
                    Rstring = [Rstring substringToIndex:2];
                    Rstring = [Rstring stringByAppendingString:@"****"];
                    
                }
                return [Rstring stringByAppendingString:[NSString stringWithFormat:@"@%@",arr[1]]];
                
            }else{
            
                return @"";
            }
            
            
        }
            break;
        case ReplaceType_ectCode:
        {
            NSRange range = NSMakeRange(2, string.length - 6);
            [string replaceCharactersInRange:range withString:@"****"];
            return string;
        }
            break;
        default:
            break;
    }
    
    for (NSInteger i = 0 ; i < replaceLength; i++) {
        
        changeStr = [changeStr stringByAppendingString:@"*"];
        
    }
    [string replaceCharactersInRange:range withString:changeStr];
    return string;
    
}


#pragma mark - 输入金额，只能是数字或小数点

+ (BOOL)isValidateAmount:(NSString *)string{
    
    NSString *regex = @"^(-)?(([1-9]{1}\\d*)|([0]{1}))(\\.(\\d){1,2})?$";
    NSPredicate *regExPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
    return [regExPredicate evaluateWithObject:string] ;
    
    
}

/**
 *  仅支持汉字、字母、数字、常规标点符号，20个字节
 *
 *  @param inputstr
 *
 *  @return YES 验证通过  NO  验证不通过
 */
+ (BOOL)containOnlyChineseEnNumber:(NSString *)inputstr {
    NSString *regex = @"^[0-9a-zA-Z\u4e00-\u9fa5.，,。？“” ]+$";
    NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
    return [pred evaluateWithObject:inputstr];
}

+ (NSString *)bankCradShow:(NSString*)string{//银行卡分段显示
    
    NSMutableString *newString = [string mutableCopy];
    
    for (int i = 0 ; i < string.length; i ++ ) {
        if (i%4 == 0 && i != 0) {
            
            [newString insertString:@" " atIndex:i/4 + i - 1];
            
        }
    }
    return newString;
}

+ (NSString *)bankCardFormat:(NSString *)string//输入时分段显示
{
    if ([string hasSuffix:@" "]) {
        return string;
    }
    NSString *result = nil;
    NSString *space = @" ";
    NSMutableString *mutableString = [[NSMutableString alloc] init];
    [mutableString appendString:string];
    NSInteger stringLength = [mutableString length];
//    if (stringLength > 4) {
//        if (stringLength%5 == 0) {
//            [mutableString insertString:space atIndex:stringLength-1];
//        }
//        result = mutableString;
//    }else{
//        result = mutableString;
//    }
    if (stringLength%5 == 4) {
        [mutableString insertString:space atIndex:stringLength];
    }
    result = mutableString;
    return result;
}

//判断NSString字符串是否包含emoji表情

+ (BOOL)stringContainsEmoji:(NSString *)string

{
    
    __block BOOL returnValue =NO;
    
    [string enumerateSubstringsInRange:NSMakeRange(0, [string length]) options:NSStringEnumerationByComposedCharacterSequences usingBlock:^(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop) {
        
        const unichar hs = [substring characterAtIndex:0];
        
        // surrogate pair
        
        if (0xd800) {
            
            if (0xd800 <= hs && hs <= 0xdbff) {
                
                if (substring.length > 1) {
                    
                    const unichar ls = [substring characterAtIndex:1];
                    
                    const int uc = ((hs - 0xd800) * 0x400) + (ls - 0xdc00) + 0x10000;
                    
                    if (0x1d000 <= uc && uc <= 0x1f77f) {
                        
                        returnValue =YES;
                        
                    }
                    
                }
                
            }else if (substring.length > 1) {
                
                const unichar ls = [substring characterAtIndex:1];
                
                if (ls == 0x20e3) {
                    
                    returnValue =YES;
                    
                }
                
            }else {
                
                // non surrogate
                
                if (0x2100 <= hs && hs <= 0x27ff) {
                    
                    returnValue =YES;
                    
                }else if (0x2B05 <= hs && hs <= 0x2b07) {
                    
                    returnValue =YES;
                    
                }else if (0x2934 <= hs && hs <= 0x2935) {
                    
                    returnValue =YES;
                    
                }else if (0x3297 <= hs && hs <= 0x3299) {
                    
                    returnValue =YES;
                    
                }else if (hs == 0xa9 || hs == 0xae || hs == 0x303d || hs == 0x3030 || hs == 0x2b55 || hs == 0x2b1c || hs == 0x2b1b || hs == 0x2b50) {
                    
                    returnValue =YES;
                    
                }
                
            }
            
        }
        
    }];
    
    return returnValue;
    
}


//// 动态获得文字的size
//- (CGSize)getTextSizeWithString:(NSString *)string font:(UIFont *)font width:(CGFloat)width
//{
//    CGSize size;
//    NSDictionary *attributesDictionary = [NSDictionary dictionaryWithObjectsAndKeys:
//                                          font, NSFontAttributeName,
//                                          nil];
//    size = [string boundingRectWithSize:CGSizeMake(width, MAXFLOAT)
//                                options:(NSStringDrawingUsesLineFragmentOrigin |NSStringDrawingUsesFontLeading)
//                             attributes:attributesDictionary context:nil].size;
//    return size;
//}

// 动态获得文字的长度
+ (CGFloat)getTextWidth:(UIFont *)font string:(NSString *)string
{
    CGSize size;
    NSDictionary *attributesDictionary = [NSDictionary dictionaryWithObjectsAndKeys:
                                          font, NSFontAttributeName,
                                          nil];
    size = [string boundingRectWithSize:CGSizeMake(MAXFLOAT, 20)
                                options:NSStringDrawingUsesLineFragmentOrigin
                             attributes:attributesDictionary context:nil].size;
    return size.width;
}


+ (NSString*)getAesEncrypt:(NSString *)encrypt
{
    BYTE *aesEncry = (BYTE*)[encrypt UTF8String];
    
    NSString *encry = [AesEncrypt_IOS encryptAESAndBase64:"^_sT<>-#g+LL@8*>" ipunt:aesEncry];
    
    return encry;
}

+ (NSString*)getDecCode:(NSString *)dec

{
    
    NSString *decCode = [AesEncrypt_IOS DecCodebase64decryptAES:"+G@Ll*<>-#y+Wb@a" input:dec];
    
    return decCode;
}


+(NSString *)URLDecodedString:(NSString *)str
{
    NSString *decodedString=(__bridge_transfer NSString *)CFURLCreateStringByReplacingPercentEscapesUsingEncoding(NULL, (__bridge CFStringRef)str, CFSTR(""), CFStringConvertNSStringEncodingToEncoding(kCFStringEncodingUTF8));
    
    return decodedString;
}

+ (NSString *)md5Hash:(NSString *)str {
    const char *cStr = [str UTF8String];
    unsigned char result[16];
    CC_MD5( cStr, (CC_LONG)strlen(cStr), result );
    NSString *md5Result = [NSString stringWithFormat:
                           @"%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
                           result[0], result[1], result[2], result[3],
                           result[4], result[5], result[6], result[7],
                           result[8], result[9], result[10], result[11],
                           result[12], result[13], result[14], result[15]
                           ];
    return md5Result;
}

+ (NSString *)randomIdentify
{
    return [FactoryClass md5Hash:[NSUUID UUID].UUIDString];
}


+ (void)deleteHeaderImage{
    
    NSString * DocumentsPath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0] copy];
    NSString *filepath = [DocumentsPath stringByAppendingString:@"/imageHeader.png"];

    NSFileManager *fileManager = [NSFileManager defaultManager];

    if ([fileManager removeItemAtPath:filepath error:nil]) {
        NSLog(@"删除头像成功,%@",filepath);
    }
    
    
}

//搜索结果显示高亮
+ (NSAttributedString *)attributeNewMessage:(NSString *)text andKeyWord:(NSString *)keyWord
{
    
    if (!text) {
        return [[NSAttributedString alloc]init];
    }
    
    NSMutableAttributedString *string = [[NSMutableAttributedString alloc]initWithString:text];
    NSDictionary *attributed = @{NSForegroundColorAttributeName: [UIColor colorWithRed:1.0 green:86 / 255.0 blue:86 / 255.0 alpha:1.0]};
    
    NSString *textCopy = [text uppercaseString];
    
    NSString *replace = @"";
    for (int i = 0 ; i < keyWord.length; i ++ ) {
        replace = [replace stringByAppendingString:@"卍"];
    }
    
    while ([textCopy rangeOfString:[keyWord uppercaseString]].location != NSNotFound) {
        NSRange range = [textCopy rangeOfString:[keyWord uppercaseString]];
        textCopy = [textCopy stringByReplacingCharactersInRange:range withString:replace];
        [string addAttributes:attributed range:range];
    }
    
    return string;
}


//获取汉字的拼
+ (NSString *)pinyinstring:(NSString *)string {
    
    NSMutableString *source = [string mutableCopy];
    CFStringTransform((__bridge CFMutableStringRef)source, NULL, kCFStringTransformMandarinLatin, NO);
    CFStringTransform((__bridge CFMutableStringRef)source, NULL, kCFStringTransformStripDiacritics, NO);
    return source;
}

+ (NSUInteger) lenghtWithString:(NSString *)string
{
    NSUInteger len = string.length;
    // 汉字字符集
    NSString * pattern= @"[\u4e00-\u9fa5]";
    NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:pattern options:NSRegularExpressionCaseInsensitive error:nil];
    // 计算中文字符的个数
    NSInteger numMatch = [regex numberOfMatchesInString:string options:NSMatchingReportProgress range:NSMakeRange(0, len)];
    
    return len + numMatch;
}

+ (NSString *)myJSONString:(id)dic{
    
    if (!dic) {
        return @"";
    }
    NSError *praseError = nil;
    NSData *data = [NSJSONSerialization dataWithJSONObject:dic options:NSJSONWritingPrettyPrinted error:&praseError];
    NSString *jsonStr = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    return jsonStr;
    
}

+ (id )myJSONVlue:(NSString *)jsonStr{
    if (!jsonStr) {
        return [[NSObject alloc]init];
    }
    NSError *praseError = nil;
    NSData *data = [jsonStr dataUsingEncoding:NSUTF8StringEncoding];
    id dic = [NSJSONSerialization JSONObjectWithData:data options:0 error:&praseError];
    return dic;
    
}

+ (NSString *)getLastFour:(NSString *)num
{
    NSString * newstr = [num substringWithRange:NSMakeRange(num.length-4, 4)];
    return newstr;
}

/**
 ** lineView:	   需要绘制成虚线的view
 ** lineLength:	 虚线的宽度
 ** lineSpacing:	虚线的间距
 ** lineColor:	  虚线的颜色
 **/
+ (void)drawDashLine:(UIView *)lineView lineLength:(int)lineLength lineSpacing:(int)lineSpacing lineColor:(UIColor *)lineColor//画一条虚线
{
    CAShapeLayer *shapeLayer = [CAShapeLayer layer];
    [shapeLayer setBounds:lineView.bounds];
    [shapeLayer setPosition:CGPointMake(CGRectGetWidth(lineView.frame) / 2, CGRectGetHeight(lineView.frame))];
    [shapeLayer setFillColor:[UIColor clearColor].CGColor];
    //  设置虚线颜色为blackColor
    [shapeLayer setStrokeColor:lineColor.CGColor];
    //  设置虚线宽度
    [shapeLayer setLineWidth:CGRectGetHeight(lineView.frame)];
    [shapeLayer setLineJoin:kCALineJoinRound];
    //  设置线宽，线间距
    [shapeLayer setLineDashPattern:[NSArray arrayWithObjects:[NSNumber numberWithInt:lineLength], [NSNumber numberWithInt:lineSpacing], nil]];
    //  设置路径
    CGMutablePathRef path = CGPathCreateMutable();
    CGPathMoveToPoint(path, NULL, 0, 0);
    CGPathAddLineToPoint(path, NULL, CGRectGetWidth(lineView.frame), 0);
    [shapeLayer setPath:path];
    CGPathRelease(path);
    //  把绘制好的虚线添加上来
    [lineView.layer addSublayer:shapeLayer];
}

+ (NSString *)getAAImagePath{
    
    NSString *document = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
    NSString *fileExists = [NSString stringWithFormat:@"%@/AddressBookImages",document];
    
    if (![[NSFileManager defaultManager] fileExistsAtPath:fileExists]) {
        
        [[NSFileManager defaultManager] createDirectoryAtPath:fileExists withIntermediateDirectories:YES attributes:nil error:nil];
        
    }
    return fileExists;
}


+ (NSString *)getFirstLetter:(NSString *)pinyin{
    
    NSString *firstLetterNew = @"";
    NSArray *array = @[@"A",@"B",@"C",@"D",@"E",@"F",@"G",@"H",@"I",@"J",@"K",@"L",@"M",@"N",@"O",@"P",@"Q",@"R",@"S",@"T",@"U",@"V",@"W",@"X",@"Y",@"Z"];
    
    if ([pinyin length]>0) {
        NSString  *firstLetter = [[pinyin substringToIndex:1]uppercaseString];
        
        for (NSString *F in array) {
            if ([firstLetter isEqualToString:F]) {
                firstLetterNew = firstLetter;
                break;
            }
        }
        //没有取到任何值返回 ＃
        if ([firstLetterNew length] == 0) {
            firstLetterNew = @"#";
        }
    }
    return firstLetterNew;
}

+ (BOOL)isChineseAndLetter:(NSString *)text{
    
    NSString *regex = @"^[a-zA-Z\u4e00-\u9fa5]+$";
    NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
    
    return [pred evaluateWithObject:text];
}

+(NSString *)moneyString:(NSString *)moneyString accurateToPosition:(NSInteger)position{
    NSDecimalNumber *originalMoneyString = [NSDecimalNumber decimalNumberWithString:moneyString];
    NSDecimalNumberHandler *handler = [NSDecimalNumberHandler decimalNumberHandlerWithRoundingMode:NSRoundPlain scale:position raiseOnExactness:NO raiseOnOverflow:NO raiseOnUnderflow:NO raiseOnDivideByZero:NO];
    NSDecimalNumber *roundedMoneyNumber = [originalMoneyString decimalNumberByRoundingAccordingToBehavior:handler];
    NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
    numberFormatter.positiveFormat = @"0.00";// 类似dateFormatter
    return [[NSString stringWithFormat:@"%@",[numberFormatter stringFromNumber:roundedMoneyNumber]] isEqualToString:@"NaN"]?@"0.00":[NSString stringWithFormat:@"%@",[numberFormatter stringFromNumber:roundedMoneyNumber]];
}

@end
